<template>
  <div class="match-index position-relative">
    <div ref="aicr" id="aicr"></div>
    <div class="text-center1" :title="isnot.msg">
      <h3 class="text-center" v-text="match.name"></h3>
      <div :style="{ width: '429px', margin: '0 auto', fontSize: '18px' }">
        <div class="time">
          比赛时间：
          <span v-text="TimeYH(match.activitystarttime)"></span>至
          <span v-text="TimeYH(match.activityendtime)"></span>
        </div>
        <div class="time">
          比赛时长：
          <span
            v-text="match.duration ? match.duration : getm(match.activitystarttime, match.activityendtime)"></span>分钟
        </div>
        <div v-if="user" class="user">
          <span>当前账号：</span>
          <span class="name">{{ user.phonenum ? user.phonenum : user.gid }}</span>
          <span @click="click" class="name2 cu">
            <el-icon color="#ffffff" :size="20">
              <Switch />
            </el-icon>
          </span>
        </div>
        <div v-else class="user">
          <span>当前账号：</span>
          <span @click="click" class="name">登录</span>
        </div>
        <div v-if="time.isok">
          <div class="times">
            距离比赛结束还有
            <span>{{ time.d }}</span>
            天
            <span>{{ time.h }}</span>
            时
            <span>{{ time.m }}</span>
            分
            <span>{{ time.s }}</span>
            秒
          </div>
        </div>
      </div>

      <div v-if="time.isok">
        <div class="text-center" v-if="Boolean(isnot.err)">
          {{ isnot.msg }}
        </div>
        <div class="text-center" v-else>
          <button :disabled="Boolean(isnot.err)" :title="isnot.msg" class="bt btn" @click="gotomatch">进入比赛</button>
        </div>
        <!-- <div class="yuedu text-center">
          <input type="checkbox" v-model="check" class="form-check-input" id="exampleCheck1" />
          我已阅读本次
          <a href="#">比赛须知</a>
        </div> -->
      </div>
      <div v-else>
        <div class="text-center">比赛已结束</div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { Switch } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from "element-plus";
import {
  computed,
  inject,
  onDeactivated,
  onMounted,
  ref,
} from "vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import { Apimatchuserquestion, asyncgetactivityduration } from "../../../api";
import { Storekey } from "../../../interface";
import Studycodemodel from "../../../mysql_interface/studycodemodel";
import Aircraft from "../../../threecanvas/aircraft";
import { TimeYH } from "../../../util/time";
type SMP = {
  time: { isok: boolean; d: number; h: number; m: number; s: number }
  getm?: (s?: Date, e?: Date) => number
  match: Studycodemodel.Iactivity
  questionbank?: Studycodemodel.Iquestionbank
  matchuser?: Apimatchuserquestion
  start: Function
  isnot: { err: number; msg: string },
  setduration: (data: { activityendtime: Date, activitystarttime: Date }) => void
}
const p = withDefaults(defineProps<SMP>(), {
  getm: () => 0,
})
const check = ref(true);
const router = useRouter();
const store = useStore(Storekey);
const headnavdialogVisible = inject("headnavdialogVisible", () => { });
const user = computed(() => {
  return store.state.user;
});

const aircraft = ref<Aircraft>();

onMounted(() => {

});
onDeactivated(() => {
  aircraft.value?.clearanimation();
});


async function gotomatch() {
  if (p.isnot.err) {
    ElMessage.warning(p.isnot.msg);
    return;
  }
  if (!check.value) {
    ElMessage.warning("请先了解比赛须知");
    return;
  }
  // if (p.match.invitation) {
  //   const _myteam = localStorage.getItem(`${user.value.ID}-${p.match.ID}`)
  //   let b = false
  //   if (_myteam) {
  //     b = p.match.invitation.split(',').includes(_myteam)
  //   }
  //   if (!b) {
  //     try {
  //       const { value } = await ElMessageBox.prompt('请输入激活码', '提示', {
  //         confirmButtonText: '确定',
  //         cancelButtonText: '查看比赛详情',
  //         distinguishCancelAndClose: true,
  //       })
  //       const t = p.match.invitation.split(',').includes(value)
  //       if (t) {
  //         localStorage.setItem(`${user.value.ID}-${p.match.ID}`, value)
  //       } else {
  //         ElMessage.error('激活码输入错误')
  //         return
  //       }
  //     } catch (e) {
  //       if (e === 'cancel') {
  //         const url = new URL(location.href)
  //         url.hash = '#/activity/' + p.match.ID
  //         window.open(url.href, '_blank')
  //       }
  //       return
  //     }
  //   }

  // }

  if (p.match.duration) {
    ElMessageBox.confirm(`是否开始答题？开始后答题时间为${p.match.duration}分钟,规定时间内必须提交答案，超时后将无法进入比赛`, '提示', {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async e => {
      const data = await asyncgetactivityduration({ activityid: p.match.ID, isstart: true })
      p.setduration(data)
      router.push({
        name: "比赛答题",
        params: {
          index: 0,
        },
      });
    }).catch(f => {
      ElMessage.error(f)
    })
  } else {
    router.push({
      name: "比赛答题",
      params: {
        index: 0,
      },
    });
  }
}

function click() {
  headnavdialogVisible()
}


</script>

<style scoped>
.text-center1 {
  width: 960px;
  height: 528px;
  background: linear-gradient(100.82deg,
      rgba(233, 232, 232, 0.8) 0.21%,
      rgba(255, 255, 255, 0.8) 100%);
  backdrop-filter: blur(5px);
  padding-top: 48px;
  border-radius: 24px;
  color: var(--color-1f);
  filter: drop-shadow(0px 4px 40px rgba(0, 0, 0, 0.16));
}

.match-index {
  min-height: 75vh;
  background-repeat: no-repeat;
  background-size: cover;

  display: flex;
  align-items: center;
  justify-content: center;
}

h3 {
  font-size: 32px;
  margin-bottom: 50px;
  font-weight: bold;
}

.time {
  margin-bottom: 20px;
}

.user {
  line-height: 26px;
  margin-bottom: 25px;
}

.user .el-avatar {
  transform: translateY(6px);
}

.name {
  height: 26px;
  background: var(--color-cheng);
  border-radius: 13px;
  display: inline-block;
  line-height: 26px;
  padding-left: 12px;
  padding-right: 12px;
  margin-left: 10px;
  margin-right: 10px;
  color: #fff;
}

.name2 {
  height: 26px;
  background: #434343;
  border-radius: 13px;
  display: inline-block;
  font-size: 16px;
  line-height: 26px;
  padding-left: 12px;
  padding-right: 12px;
  margin-left: 10px;
  margin-right: 10px;
  padding-top: 2px;
}

.times {
  margin-top: 10px;
  margin-bottom: 48px;

  line-height: 24px;
}

.times span {
  display: inline-block;
  height: 24px;
  background: #434343;
  border-radius: 2px;
  text-align: center;
  color: #fff;
  padding-left: 4px;
  padding-right: 4px;
}

.bt {
  width: 182px;
  height: 50px;
  background: var(--color-cheng);
  border-radius: 25px;
  font-size: 24px;
  font-family: PingFang SC;
  color: #fff;
}

.bt:hover {
  color: #ffffff;
}

.yuedu {
  margin-top: 12px;
  font-size: 14px;
  font-weight: 400;
}

.yuedu a {
  font-weight: 800;
  color: #434343;
}

#aicr {
  position: fixed;
  height: 100vh;
  width: 100vw;
  background: linear-gradient(#e4e0ba, #f7d9aa);
}
</style>
